package com.home.base.util;

import org.openqa.selenium.By;
import org.openqa.selenium.PageLoadStrategy;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.springframework.util.ObjectUtils;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

public class SeleniumFetchUtil {
   static SeleniumFetchUtil instance;
    ChromeDriver webDriver;
    String baseUri="https://cbss.10010.com/essframe";

    public SeleniumFetchUtil(){
        if(ObjectUtils.isEmpty(webDriver)){
            this.webDriver=init();


            //webDriver.executeCdpCommand("Page.addScriptToEvaluateOnNewDocument", params);
        }
    }

    public static SeleniumFetchUtil getInstance(){
        if(ObjectUtils.isEmpty(instance)){
            instance=new SeleniumFetchUtil();
        }
        return instance;
    }

    public ChromeDriver init(){
        System.setProperty("webdriver.chrome.driver",  "f://wb//360//chromedriver.exe");
        ChromeOptions options = new ChromeOptions();
        options.setPageLoadStrategy(PageLoadStrategy.EAGER);
        options.setCapability(CapabilityType.ACCEPT_SSL_CERTS,true);
        options.setCapability(CapabilityType.ACCEPT_INSECURE_CERTS,true);
        options.addArguments("start-maximized"); // https://stackoverflow.com/a/26283818/1689770
        options.addArguments("enable-automation"); // https://stackoverflow.com/a/43840128/1689770
        //options.addArguments("--headless"); // only if you are ACTUALLY running headless
        options.addArguments("--no-sandbox"); //https://stackoverflow.com/a/50725918/1689770
        options.addArguments("--disable-infobars"); //https://stackoverflow.com/a/43840128/1689770
        options.addArguments("--disable-dev-shm-usage"); //https://stackoverflow.com/a/50725918/1689770
        options.addArguments("--disable-browser-side-navigation"); //https://stackoverflow.com/a/49123152/1689770
        options.addArguments("--disable-blink-features");
        options.addArguments("--disable-blink-features=AutomationControlled");
        options.addArguments("--disable-gpu");

        options.setExperimentalOption("excludeSwitches", Collections.singletonList("enable-automation"));

        options.setExperimentalOption("useAutomationExtension", false);
        options.setBinary("C:\\Users\\hcyx\\AppData\\Roaming\\secoresdk\\360se6\\Application\\360se.exe");
        return new ChromeDriver(options);
    }

    public ChromeDriver getWebDriver(){
        if(ObjectUtils.isEmpty(webDriver)){
            return init();
        }
        return this.webDriver;
    }

    public void open( ) {
        webDriver.get(baseUri);
        getWebDriver().manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        System.out.println(webDriver.getPageSource());
        new WebDriverWait(getWebDriver(), 20).until(
                ExpectedConditions.elementToBeClickable(By.xpath("//html")));
        System.out.println("ttt:"+getWebDriver().getTitle());
    }

    public void source( ) {
        System.out.println(" this is wait");
//        new WebDriverWait(getWebDriver(), 20).until(
//                ExpectedConditions.elementToBeClickable(By.xpath("//html")));
        System.out.println(" this is ok");
         //getWebDriver().get(baseUri);
        //getWebDriver().manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        try {
//            WebDriverWait wait = new WebDriverWait(getWebDriver(), 10);
//            wait.wait();
            System.out.println(getWebDriver().getTitle());
            System.out.println(getWebDriver().getCurrentUrl());
            System.out.println(getWebDriver().getPageSource());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public static void main(String[] args){
        //SeleniumFetchUtil.getInstance().loginYY("","");
    }
}
